package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService service;

    /**
     * 登录
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){

        //获取用户名密码
        String username = employee.getUsername();
        String password = employee.getPassword();

        //密码加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //判断用户名是否存在
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,username);
        Employee emp = service.getOne(lqw);
        if (emp==null){
            return R.error("用户名或密码错误");
        }

        //判断密码是否匹配
        if (!emp.getPassword().equals(password)){
            return R.error("用户名或密码错误");
        }

        //判断是否被禁用
        if (emp.getStatus()==0){
            return R.error("用户被禁用");
        }

        //id存储到session
        request.getSession().setAttribute("employee",emp.getId());

        return R.success(employee);
    }

    /**
     * 退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){

        //请求存储的用户
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加员工
     * @param employee
     * @param request
     * @return
     */
    @PostMapping()
    public R<String> addEmployee(@RequestBody Employee employee,HttpServletRequest request){

        //获取当前操作员工
        Long empId = (Long) request.getSession().getAttribute("employee");

        //添加employee信息
//        employee.setUpdateUser(empId);
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setCreateUser(empId);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //调用添加方法，返回成功
        service.save(employee);
        return R.success("添加成功");

    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Long page, Long pageSize, String name){

        //分页构造器
        Page<Employee> pageInfo = new Page<>(page,pageSize);

        //条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //判断姓名是否输入
        lqw.eq(!StringUtils.isEmpty(name),Employee::getUsername,name);
        lqw.orderByAsc(Employee::getUpdateTime);

        //调用分页查询方法
        service.page(pageInfo,lqw);

        //返回查询结果
        return R.success(pageInfo);
    }

    /**
     * 修改员工信息
     * @param employee
     * @param request
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpServletRequest request){

        //获取修改人
        Long empId = (Long) request.getSession().getAttribute("employee");

        //修改员工信息
        //时间，修改人，状态(发送过来的是修改过的)
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        employee.setStatus(employee.getStatus());

        //调用修改方法
        service.updateById(employee);

        //返回信息
        return R.success("修改成功");
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){

        //根据id，查询对应员工信息
        Employee emp = service.getById(id);

        //判断id是否为空
        if (emp == null){
            return R.error("查无用户");
        }

        //返回员工信息
        return R.success(emp);
    }
}
